package leetcode_201_300;

import java.util.*;

public class LeeCode_264 {
    public static void main(String[] args) {
        System.out.println(nthUglyNumber(4));
    }
    private static int nthUglyNumber(int n) {
        int[] dp = new int[n + 1];
        dp[1] = 1;
        int p2 = 1, p3 = 1, p5 = 1;
        for (int i = 2; i < n + 1; i++) {
            int n2 = dp[p2] * 2, n3 = dp[p3] * 3, n5 = dp[p5] * 5;
            dp[i] = Math.min(Math.min(n2, n3), n5);
            if (dp[i] == n2){
                p2++;
            }
            if (dp[i] == n3){
                p3++;
            }
            if (dp[i] == n5){
                p5++;
            }
        }
        return dp[n];
    }
}
